const myaudio = wx.createInnerAudioContext();
const db = wx.cloud.database()
const _ = db.command
let lyric = '',
    InnerAudioContext = wx.createInnerAudioContext(),
    address = ''
Page({

    data: {
        lyricShow: false,
        isplay: true, //是否播放
    },


    clickShow: function () { // 歌词显示
        this.setData({
            lyricShow: !this.data.lyricShow
        })
    },

    music() { // 播放暂停
        let {
            isplay
        } = this.data
        if (isplay) {
            InnerAudioContext.pause();
            this.setData({
                isplay: false
            });
            console.log(isplay);
        } else {
            InnerAudioContext.play();
            console.log(isplay);
            this.setData({
                isplay: true
            });
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        let word = JSON.parse(options.word)

        new Promise(() => { // 单词加粗，提取中文翻译并加样式
            if (word.lyric) {
                let lyricTex = `<p>${word.lyric}</p>`.replaceAll(word.content, `<span style="font-weight:bold;">${word.content}</span>`) // 单词加粗
                let reg = lyricTex.match(/[\u4e00-\u9fa5]/g).join('') // 提取汉字
                // console.log('reg', reg)
                lyric = lyricTex.replaceAll(reg, `<span style='font-size:17px; font-family:Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;'><br>${reg}<br></span>`)
            }
        })

        address = word.address
        console.log(address)
        this.setData({
            lyric:lyric,
            user: word.user || ''
        });

        this.playMusic(address)
    },

    playMusic(address) {
        if (address) {
            InnerAudioContext.src = address
            InnerAudioContext.play();
            InnerAudioContext.onEnded(() => {
                this.setData({ // 播放结束后转为播放键
                    isplay: false,
                });
            })
        }
    },

    onUnload() {
        InnerAudioContext.stop()
        // InnerAudioContext.destroy() 
        this.setData({
            isplay: false
        })
    }
})